﻿using System;
using System.Data;
using System.Reflection;
using System.Web.Security;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using Microsoft.ApplicationBlocks.Data;
namespace AnyOffice.general.system.user
{
    public partial class user_new : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);     
        protected string strTemp;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
               
                ListItem item = new ListItem("请选择", "");
                this.ddlDeptId.Items.Add(item);
                DataTable table = new DepartmentDao().SelectParentAll(base.Conn);
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        item = new ListItem("|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                        this.ddlDeptId.Items.Add(item);
                        this.strTemp = "";
                        this.GetDept(ref this.ddlDeptId, int.Parse(table.Rows[i]["DeptId"].ToString()), this.strTemp);
                    }
                }
                this.ddlDeptId.Items.Add(new ListItem("离校人员/外部人员", "-1"));
                item = new ListItem("请选择", "");
                this.ddlUserPriv.Items.Add(item);
                DataTable table2 = new UserPrivDao().SelectAll(base.Conn);
                if (table2.Rows.Count > 0)
                {
                    for (int j = 0; j < table2.Rows.Count; j++)
                    {
                        item = new ListItem(table2.Rows[j]["PrivName"].ToString(), table2.Rows[j]["PrivId"].ToString());
                        this.ddlUserPriv.Items.Add(item);
                    }
                }
                this.ddlDutyTypeId.DataSource = this.GetdDutyData();
                this.ddlDutyTypeId.DataBind();
                DataSet set = new DataSet();
                set.ReadXml(Server.MapPath("/theme2009/theme.xml"));
                this.ddlTheme.DataSource = set.Tables["Theme"];
                this.ddlTheme.DataBind();
            }
            if (this.Page.IsPostBack)
            {
                UsersDao dao3 = new UsersDao();
                if (dao3.IsExists(base.Conn, this.txtUserId.Text))
                {
                    this.lblError.Text = "用户名或别名" + this.txtUserId.Text + "已存在";
                }
                else
                {
                    this.pnlMain.Visible = false;
                    UsersData data = new UsersData();
                    data.UserId = this.txtUserId.Text;
                    data.UserNo = Convert.ToInt32(this.txtUserNo.Text);
                    data.UserName = this.txtUserName.Text;
                    data.DeptId = Convert.ToInt32(this.ddlDeptId.SelectedValue);
                    data.PrivId = Convert.ToInt32(this.ddlUserPriv.SelectedValue);
                    data.PostPriv = this.ddlPostPriv.Value;
                    if (this.ddlPostPriv.Value == "2")
                    {
                        data.PostDept = this.ToId.Value;
                    }
                    data.DutyType = Convert.ToInt32(this.ddlDutyTypeId.SelectedValue);
                    if (this.txtAttendCard.Text != string.Empty)
                    {
                        data.AttendCard = this.txtAttendCard.Text;
                    }
                    data.EmailCapacity = Convert.ToInt32(this.txtEmailCapacity.Text);
                    data.FolderCapacity = Convert.ToInt32(this.txtFolderCapacity.Text);
                    data.UsingKey = this.ckbUsingKey.Checked;
                    if (this.txtBindIp.Text != string.Empty)
                    {
                        data.BindIp = this.txtBindIp.Text;
                    }
                    string str3 = string.Empty;
                    string str4 = string.Empty;
                    DataTable myTable = ApplicationCache.MyTable;
                    for (int k = 0; k < myTable.Rows.Count; k++)
                    {
                        if (Convert.ToInt32(myTable.Rows[k]["ViewType"].ToString()) != 3)
                        {
                            if (myTable.Rows[k]["ModulePos"].ToString() == "l")
                            {
                                str3 = str3 + myTable.Rows[k]["ModuleId"].ToString() + ",";
                            }
                            else
                            {
                                str4 = str4 + myTable.Rows[k]["ModuleId"].ToString() + ",";
                            }
                        }
                    }
                    if (str3.EndsWith(","))
                    {
                        str3 = str3.Substring(0, str3.Length - 1);
                    }
                    if (str4.EndsWith(","))
                    {
                        str4 = str4.Substring(0, str4.Length - 1);
                    }
                    data.MytableLeft = str3;
                    data.MytableRight = str4;
                    data.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPassword.Text, "md5");
                    data.Sex = Convert.ToBoolean(this.ddlSex.SelectedValue);
                    data.Birthday = Convert.ToDateTime(this.ddlBirthday_Year.Value + "-" + this.ddlBirthday_Mon.Value + "-" + this.ddlBirthday_Day.Value);
                    data.IsHideBirthday = this.ckbIsHideBirthday.Checked;
                    data.Theme = Convert.ToInt32(this.ddlTheme.SelectedValue);
                    if (this.txtMobile.Text != string.Empty)
                    {
                        data.Mobile = this.txtMobile.Text;
                    }
                    data.IsHideMobile = this.ckbIsHideMobile.Checked;
                    try
                    {
                        dao3.Insert(base.Conn, data);
                        if (this.txtUserId2.Text != string.Empty)
                        {
                            if (this.txtUserId.Text == this.txtUserId2.Text)
                            {
                                this.litUserId2.Text = base.ShowMessage("用户名和别名不能相同");
                            }
                            else if (!dao3.SetUserId2(base.Conn, this.txtUserId.Text, this.txtUserId2.Text))
                            {
                                this.litUserId2.Text = base.ShowMessage("用户名或别名 " + this.txtUserId2.Text + " 已存在");
                            }
                        }
                        string str5 = this.txtUserName.Text + ",UserId=" + this.txtUserId.Text;
                        SysLog.Log(base.Conn, 6, str5);
                    }
                    catch (Exception exception)
                    {
                        if (log.IsErrorEnabled)
                        {
                            log.Error("", exception);
                        }
                        this.litMessage.Text = base.ShowError("添加用户失败!");
                        return;
                    }
                    this.litMessage.Text = base.ShowPrompt("添加用户成功!", "user_new.aspx");
                }
            }

        }
        private void GetDept(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.GetDept(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }

        private DataTable GetdDutyData()
        {
            string commandText = "SELECT [DutyTypeId],[DutyName] FROM [dbo].[Attend_Config] ORDER BY [DutyTypeId] ASC";
            DataSet set = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText);
            set.Tables[0].TableName = "Attend_Config";
            return set.Tables[0];
        }
    }
}
